package com.lwj.liwanjia.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.DingBean01;
import com.lwj.liwanjia.bean.JinRiDingDanHomeBean;
import com.lwj.liwanjia.config.OnClickPosition;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.utils.MyRecyclerView;
import com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan.DaiFaHuoXiangQingActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan.DingDanXiangQingHomeActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan.WuLiuActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan.tui_huo_tui_kuan.ShenQingXiangQingActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan.tui_huo_tui_kuan.THXQActivity;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 001 on 2017/9/21.
 */

public class DaiFaHuoAdapter extends RecyclerView.Adapter<DaiFaHuoAdapter.ViewHolder> {

    private Context mContext;
    private List<DingBean01.DataBean> mList;
    private OnClickPosition clickPosition;

    public void setClickPosition(OnClickPosition clickPosition) {
        this.clickPosition = clickPosition;
    }

    public DaiFaHuoAdapter(Context mContext, List<DingBean01.DataBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public DaiFaHuoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_jin_ri_ding_dan_home, parent, false);

        final ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final DaiFaHuoAdapter.ViewHolder holder, final int position) {
        final DingBean01.DataBean bean = mList.get(position);
        Glide.with(mContext).load(bean.getGoods().getHead()).into(holder.circle_photo);
        holder.tv_name.setText(bean.getGoods().getNick());
        String status = bean.getGoods().getStatus();
        if(status.equals("退款退货中")||status.equals("换货中")||status.equals("退款中")){
            holder.tv_zhuangTai.setText(bean.getGoods().getStatus());
        }else{
            holder.tv_zhuangTai.setText(bean.getGoods().getStatus().replace("中", ""));
        }
        holder.tv_heJi.setText("共" + bean.getGoods().getAmount() + "件商品 合计:￥" + bean.getGoods().getTotalpay() + "(含运费￥0.00)");
        LinearLayoutManager manager = new LinearLayoutManager(mContext) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.rlvSj.setLayoutManager(manager);
        final SjIndentAdapter adapter = new SjIndentAdapter(mContext, bean.getGoods().getCommodity());
        holder.rlvSj.setAdapter(adapter);
        adapter.setListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int p) {
                if (clickPosition != null) {
                    clickPosition.clickPosition(position);
                }
                String state = holder.tv_zhuangTai.getText().toString().trim();
                switch (state) {
                    case "待发货":
                    case "待收货":
                    case "已完成":
                    case "待付款":
                        Intent daiFaHuoIntent = new Intent(mContext, DingDanXiangQingHomeActivity.class);
                        daiFaHuoIntent.putExtra("dingDanState", state);
                        daiFaHuoIntent.putExtra("dingDanHao", bean.getGoods().getTradeId());
                        mContext.startActivity(daiFaHuoIntent);
                        break;
                    case "换货中":
                    case "已换货":
                    case "退款退货中":
                    case "已退款退货":
                        Intent tuiKuanIntent = new Intent(mContext, WuLiuActivity.class);
                        tuiKuanIntent.putExtra("dingDanState", state);
                        tuiKuanIntent.putExtra("dingDanHao", bean.getGoods().getTradeId());
                        mContext.startActivity(tuiKuanIntent);
                        break;
                    case "正在申请(待发货)":
                    case "正在申请(退款)":
                    case "正在申请(换货)":
                    case "正在申请(退款退货)":
                        Intent intent01 = new Intent(mContext, ShenQingXiangQingActivity.class);
                        intent01.putExtra("tuiState", state);
                        intent01.putExtra("id", bean.getGoods().getTradeId());
                        mContext.startActivity(intent01);
                        break;
                }
            }
        });

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickPosition != null) {
                    clickPosition.clickPosition(position);
                }
                //根据订单状态跳转到相应界面
                String state = holder.tv_zhuangTai.getText().toString().trim();
                switch (state) {
                    case "待发货":
                    case "待收货":
                    case "已完成":
                    case "待付款":
                        Intent daiFuKuanIntent = new Intent(mContext, DingDanXiangQingHomeActivity.class);
                        daiFuKuanIntent.putExtra("dingDanState", state);
                        daiFuKuanIntent.putExtra("dingDanHao", bean.getGoods().getTradeId());
                        mContext.startActivity(daiFuKuanIntent);
                        break;
                    case "换货中":
                    case "已换货":
                    case "退款退货":
                    case "已退款退货":
                        Intent tuiKuanIntent = new Intent(mContext, WuLiuActivity.class);
                        tuiKuanIntent.putExtra("dingDanState", state);
                        tuiKuanIntent.putExtra("dingDanHao", bean.getGoods().getTradeId());
                        mContext.startActivity(tuiKuanIntent);
                        break;
                    case "正在申请(待发货)":
                    case "正在申请(退款)":
                    case "正在申请(换货)":
                    case "正在申请(退款退货)":
                        Intent intent01 = new Intent(mContext, ShenQingXiangQingActivity.class);
                        intent01.putExtra("tuiState", state);
                        intent01.putExtra("id", bean.getGoods().getTradeId());
                        mContext.startActivity(intent01);
                        break;
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        View view;
        CircleImageView circle_photo;//头像
        ImageView iv_huoDaoFuKuan;//货到付款图片
        TextView tv_zhuangTai;//状态
        RelativeLayout rl_bottom;//底部按钮
        TextView tv_left;//底部 左边 按钮
        TextView tv_right;//底部 右边 按钮
        TextView tv_name;
        ImageView iv_photo;
        TextView tv_title;
        TextView tv_color;
        TextView tv_money;
        TextView tv_number;
        TextView tv_heJi;
        MyRecyclerView rlvSj;

        public ViewHolder(View itemView) {
            super(itemView);

            view = itemView;
            circle_photo = (CircleImageView) itemView.findViewById(R.id.circle_photo);
            iv_huoDaoFuKuan = (ImageView) itemView.findViewById(R.id.iv_huoDaoFuKuan);
            tv_zhuangTai = (TextView) itemView.findViewById(R.id.tv_zhuangTai);
            rl_bottom = (RelativeLayout) itemView.findViewById(R.id.rl_bottom);
            tv_left = (TextView) itemView.findViewById(R.id.tv_left_bottom);
            tv_right = (TextView) itemView.findViewById(R.id.tv_right_bottom);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            iv_photo = (ImageView) itemView.findViewById(R.id.iv_photo);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_color = (TextView) itemView.findViewById(R.id.tv_color);
            tv_money = (TextView) itemView.findViewById(R.id.tv_money);
            tv_number = (TextView) itemView.findViewById(R.id.tv_number01);
            tv_heJi = (TextView) itemView.findViewById(R.id.tv_heJi);
            rlvSj = (MyRecyclerView) itemView.findViewById(R.id.rlv_sj);
        }
    }

}
